package com.wanmait.recruitment.webController;

import com.wanmait.recruitment.mapper.EducationMapper;
import com.wanmait.recruitment.pojo.*;
import com.wanmait.recruitment.service.*;
import org.apache.commons.io.FilenameUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


@Controller
@RequestMapping("zhuang/user")
public class ResumeController {

    @Resource
    private ResumeService resumeService;
    @Resource
    private DegreeService degreeService;
    @Resource
    private ExRequireService exRequireService;
    @Resource
    private WorkingConditionService workingConditionService;
    @Resource
    private CityService cityService;
    @Resource
    private NatureService natureService;
    @Resource
    private  MoneyService moneyService;
    @Resource
    private  WorkExService workExService;
    @Resource
    private  ItemExService itemExService;
    @Resource
    private  EducationService educationService;
    @Resource
    private  WorksShowService worksShowService;
    @Resource
    private  NationsService nationsService;
    @Resource
    private  PoliticalStatusService politicalStatusService;

    @Resource
    private  HopeJobService hopeJobService;
    @RequestMapping("jianli")
    //@ModelAttribute("resume")
    public ModelAndView resumeList(HttpServletRequest request,HttpServletResponse response){
        HttpSession session=request.getSession();
       Userinfo userinfo=(Userinfo)session.getAttribute("userinfo");
          ModelAndView modelAndView=new ModelAndView();


            Resume resume= (Resume) resumeService.findById(userinfo.getId());
            List<Degree>degreeList=degreeService.findAll();
            List<ExRequire>exRequireList=exRequireService.findAll();
            List<WorkingCondition>workingConditionList=workingConditionService.findAll();
            modelAndView.addObject("resume",resume);
            modelAndView.addObject("degreeList",degreeList);
            modelAndView.addObject("exRequireList",exRequireList);
            modelAndView.addObject("workingConditionList",workingConditionList);
            List<City> cityList = cityService.findAll();
            List<City> cityAF = cityService.selectAF();
            List<City> cityGJ = cityService.selectGJ();
            List<City> cityKN = cityService.selectKN();
            List<City> cityOR = cityService.selectOR();
            List<City> citySV = cityService.selectSV();
            List<City> cityWZ = cityService.selectWZ();
            List<Nations>nationsList=nationsService.findAll();
            List<PoliticalStatus>politicalStatusList=politicalStatusService.findAll();
            List<Resume>resumeList=resumeService.selectHopeJobById(userinfo.getId());
            List<Resume>workExList=resumeService.selectWorkExById(userinfo.getId());
            List<Resume>educationLsit=resumeService.selectEducationById(userinfo.getId());

            modelAndView.addObject("cityList",cityList);
            modelAndView.addObject("cityAF",cityAF);
            modelAndView.addObject("cityGJ",cityGJ);
            modelAndView.addObject("cityKN",cityKN);
            modelAndView.addObject("cityOR",cityOR);
            modelAndView.addObject("citySV",citySV);
            modelAndView.addObject("cityWZ",cityWZ);
            modelAndView.addObject("nationsList",nationsList);
            modelAndView.addObject("politicalStatusList",politicalStatusList);
            modelAndView.addObject("workExList",workExList);
            modelAndView.addObject("educationLsit",educationLsit);


            List<Nature>natureList=natureService.findAll();
            modelAndView.addObject("natureList",natureList);
            List<Money>moneyList=moneyService.findAll();
            modelAndView.addObject("moneyList",moneyList);
            modelAndView.addObject("resumeList",resumeList);

            List<Resume>ItemExLsit=resumeService.selectItemEXById(userinfo.getId());
            modelAndView.addObject("ItemExLsit",ItemExLsit);


            return modelAndView;


    }
    @PostMapping("resumeNameForm")
    @ResponseBody
   public  void resumeNameForm(String resumeName,HttpServletRequest request){

        HttpSession session=request.getSession();
        Userinfo userinfo2=(Userinfo)session.getAttribute("userinfo");

        resumeService.updateResumeName(resumeName,userinfo2.getId());


   }
   @PostMapping("jianliform")
   public  String jianliform(@Param("file") MultipartFile file,HttpServletRequest request){
       if (file != null && !file.isEmpty()) {
           //获得物理路径
           String realPath = request.getServletContext().getRealPath("/static/jianliImages");
           //原文件名
           String originalFilename = file.getOriginalFilename();
           //获得扩展名
           String ext = FilenameUtils.getExtension(originalFilename);

           //唯一文件夹
           String fileName = UUID.randomUUID() + "." + ext;
           File outFile = new File(realPath, fileName);

           try {
               file.transferTo(outFile);

//                resume.setUserinfo(userinfo1);
               //novel.setLogoImage(fileName);
               System.out.println(fileName);
           } catch (IOException e) {
               e.printStackTrace();
           }
       }

       return "redirect:/zhuang/user/jianli";
   }
   @PostMapping("modifyData")
    public String modifyData(@Param("file") MultipartFile file,HttpServletRequest request,String name,String sex,String dergges,String exRequire,String telephone,String email,String working,String nationstitle,String politicalStatus,String nativePlace) {
       HttpSession session = request.getSession();
       Userinfo userinfo2 = (Userinfo) session.getAttribute("userinfo");
       int count = resumeService.dataCountResume(userinfo2.getId());
       Userinfo userinfo1 = new Userinfo();
       if (file != null && !file.isEmpty()) {
           //获得物理路径
           String realPath = request.getServletContext().getRealPath("/static/uploadImages");
           //原文件名
           String originalFilename = file.getOriginalFilename();
           //获得扩展名
           String ext = FilenameUtils.getExtension(originalFilename);

           //唯一文件夹
           String fileName = UUID.randomUUID() + "." + ext;
           File outFile = new File(realPath, fileName);

           try {
               file.transferTo(outFile);
               userinfo1.setHeadImage(fileName);
//                resume.setUserinfo(userinfo1);
               //novel.setLogoImage(fileName);
               System.out.println(fileName);
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       Resume resume = new Resume();
       resume.setName(name);
       Userinfo userinfo = new Userinfo();
       PoliticalStatus politicalStatus1=new PoliticalStatus();
       politicalStatus1.setId(Integer.parseInt(politicalStatus));
       userinfo.setPoliticalStatus(politicalStatus1);
       Nations nations =new Nations();
       nations.setId(Integer.parseInt(nationstitle));
       userinfo.setNations(nations);
       userinfo.setNativePlace(nativePlace);
       userinfo.setSex(sex);
       Degree degree = new Degree();
       degree.setId(Integer.parseInt(dergges));
       userinfo.setDegree(degree);
       userinfo.setEmail(email);
       userinfo.setTelephone(telephone);
       userinfo.setHeadImage(userinfo1.getHeadImage());
       ExRequire exRequire1 = new ExRequire();
       exRequire1.setId(Integer.parseInt(exRequire));
       resume.setExRequire(exRequire1);
       WorkingCondition workingCondition = new WorkingCondition();
       workingCondition.setId(Integer.parseInt(working));
       resume.setWorkingCondition(workingCondition);
       resume.setUserinfo(userinfo);


       if (count != 0) {
           resume.setUserinfo(userinfo);
           resumeService.uptedateResume(resume, userinfo2.getId());
           return "redirect:/zhuang/user/jianli";
       }

         else{
           //userinfo.setId(userinfo2.getId());

           resumeService.insert(userinfo2.getId(), name, Integer.parseInt(exRequire));
           resumeService.update(resume, userinfo2.getId());
           return "redirect:/zhuang/user/jianli";
       }



    }
    @PostMapping("updateWork")
    @ResponseBody
    public  void updateWork(Integer resumeId,String cityId,String natureId,String moneyId,String work){
       HopeJob hopeJob=new HopeJob();
       Resume resume=new Resume();
       resume.setId(resumeId);
       hopeJob.setResume(resume);
       City city=new City();
       city.setId(Integer.parseInt(cityId));
       hopeJob.setCity(city);
       Nature nature=new Nature();
       nature.setId(Integer.parseInt(natureId));
       hopeJob.setNature(nature);
       Money money=new Money();
       money.setId(Integer.parseInt(moneyId));
       hopeJob.setMoney(money);
       hopeJob.setJobName(work);

       hopeJobService.insert(hopeJob);




    }

    @PostMapping("insertWorkEx")
    @ResponseBody
    public void insertWorkEx(Integer resumeId,String companyName,String positionName,String stattime,String statMonth,String endyear,String endmonth){

        WorkEx workEx=new WorkEx();
        Resume resume=new Resume();
        resume.setId(resumeId);
        workEx.setResume(resume);
        workEx.setCompanyName(companyName);
        workEx.setJobName(positionName);
        String time=stattime+"-"+statMonth;
        String endTime=endyear+"-"+endmonth;
        workEx.setTimePeriod(time);
        workEx.setEndTimePeriod(endTime);

        workExService.insert(workEx);



    }
    @PostMapping("insertItemEx")
    @ResponseBody
    public  void insertItemEx(Integer resumeId,String projectNames,String thePost,String statyear,String statmonth,String endyear , String endmonth,String describe){
        ItemEx itemEx=new ItemEx();
        itemEx.setItemName(projectNames);
        itemEx.setJob(thePost);
        String time=statyear+"-"+statmonth;
        itemEx.setTimePeriod(time);
        String endTime=endyear+"-"+endmonth;
        itemEx.setEndTimePeriod(endTime);
        itemEx.setDescribe(describe);
        Resume resume=new Resume();
        resume.setId(resumeId);
        itemEx.setResume(resume);

        itemExService.insert(itemEx);
    }
    @PostMapping("insertEducation")
    @ResponseBody
    public  void insertEducation(Integer resumeId,String schoolName,String degreeId,String professionalName,String statyear,String statmonth,String smonth,String emonth){

        Education education=new Education();
        education.setSchool(schoolName);
        Degree degree=new Degree();
        degree.setId(Integer.parseInt(degreeId));
        education.setDegree(degree);
        education.setProfessional(professionalName);
        Resume resume=new Resume();
        resume.setId(resumeId);
        education.setResume(resume);

        String time=statyear+"-"+smonth;
        String endTime=statmonth+"-"+emonth;
        education.setTimePeriod(time);
        education.setEndTimePeriod(endTime);
        educationService.insertEducation(education);

    }
    @PostMapping("updateSelf")
    @ResponseBody
    public  void updateSelf(String self,HttpServletRequest request){
        HttpSession session=request.getSession();
        Userinfo userinfo2=(Userinfo)session.getAttribute("userinfo");
       resumeService.updateSelf(self,userinfo2.getId());

    }
    @PostMapping("updateWroksShow")
    @ResponseBody
    public  void updateWroksShow(HttpServletRequest request,String workLink,String workDescription){
        HttpSession session=request.getSession();
        Userinfo userinfo2=(Userinfo)session.getAttribute("userinfo");

        Resume resume=new Resume();
        WorksShow worksShow=new WorksShow();
        worksShow.setConnect(workLink);
        worksShow.setIntroduce(workDescription);
        resume.setWorksShow(worksShow);

        int count=resumeService.dataCountWorksShow(userinfo2.getId());
        if(count==0){

            resumeService.updateWorksShow(resume,userinfo2.getId());
        }
        else {
            worksShowService.insert(worksShow);
            int counts=worksShowService.count();
           resumeService.updateWorksShowId(counts,userinfo2.getId());

        }
    }
    @RequestMapping("preview")
    public  ModelAndView preview(Integer id){
        ModelAndView modelAndView=new ModelAndView();
        Resume resume=resumeService.findAllByIdResume(id);
        List<HopeJob>hopeJobs=hopeJobService.selectById(id);
        List<WorkEx>workExList=workExService.selectList(id);
        List<Education>educationList=educationService.findListById(id);
        List<ItemEx>itemExList=itemExService.findByIdList(id);
        modelAndView.addObject("resume", resume);
        modelAndView.addObject("hopeJobs",hopeJobs);
        modelAndView.addObject("workExList",workExList);
        modelAndView.addObject("educationList",educationList);
        modelAndView.addObject("itemExList",itemExList);
        return  modelAndView;

    }
    @RequestMapping("jianliDetailed")
    public  ModelAndView jianliDetailed(Integer id){
        ModelAndView modelAndView=new ModelAndView();
        List<City>cityList=cityService.selectCityTop();

        List<Nature>natureList=natureService.findAll();
        List<Money>moneyList=moneyService.findAll();
        HopeJob hopeJob=hopeJobService.findById(id);
        modelAndView.addObject("hopeJob",hopeJob);
        modelAndView.addObject("cityList",cityList);
        modelAndView.addObject("natureList",natureList);
        modelAndView.addObject("moneyList",moneyList);
        return modelAndView;

    }
    @RequestMapping("jianliDetailedWorkEx")
    public ModelAndView jianliDetailedWorkEx(Integer id){

        ModelAndView modelAndView=new ModelAndView();
        WorkEx workEx=workExService.findById(id);
        modelAndView.addObject("workEx",workEx);

        return  modelAndView;
    }
    @RequestMapping("jianliDetailedItemEx")
    public  ModelAndView jianliDetailedItemEx(Integer id){
        ModelAndView modelAndView=new ModelAndView();
        ItemEx itemEx=itemExService.findById(id);
        modelAndView.addObject("itemEx",itemEx);

        return  modelAndView;

    }
    @RequestMapping("jianliDetailedEducation")
    public  ModelAndView jianliDetailedEducation(Integer id){
        ModelAndView modelAndView=new ModelAndView();
        Education education=educationService.findById(id);
        List<Degree>degreeList=degreeService.findAll();
        modelAndView.addObject("education",education);
        modelAndView.addObject("degreeList",degreeList);

        return  modelAndView;

    }
   @PostMapping("updateHopeJob")
    @ResponseBody
    public  void updateHopeJob(Integer jobId,Integer cityId,Integer natureId,Integer moneyId,String job){

        HopeJob hopeJob=new HopeJob();
        City city=new City();
        city.setId(cityId);
        hopeJob.setCity(city);
        Nature nature=new Nature();
        nature.setId(natureId);
        hopeJob.setNature(nature);
        Money money=new Money();
        money.setId(moneyId);
        hopeJob.setMoney(money);
        hopeJob.setJobName(job);
        hopeJobService.update(hopeJob,jobId);

    }
    @PostMapping("updateWorkEx")
    @ResponseBody
    public  void updateWorkEx(WorkEx workEx,Integer id){

        workExService.update(workEx,id);
    }
    @PostMapping("updateItemEx")
    @ResponseBody
    public  void updateItemEx(ItemEx itemEx,Integer id){
        System.out.println(itemEx+""+id);
        itemExService.update(itemEx,id);

        //workExService.update(itemEx,id);
    }
    @PostMapping("updateEducation")
    @ResponseBody
    public  void updateEducation(Education education,Integer id){
        System.out.println(education);
        System.out.println(id);
        educationService.update(education,id);

    }
    @PostMapping("deleteHopeJob")
    @ResponseBody
    public  void deleteHopeJob(Integer id){
          hopeJobService.delete(id);
    }


    @PostMapping("deleteWorkEx")
    @ResponseBody
    public  void deleteWorkEx(Integer id){
        workExService.delete(id);

    }


    @PostMapping("deleteItemEx")
    @ResponseBody
    public  void deleteItemEx(Integer id){
        //workExService.delete(id);
        itemExService.delete(id);

    }

    @PostMapping("deleteEducation")
    @ResponseBody
    public  void deleteEducation(Integer id){
        //workExService.delete(id);
        educationService.delete(id);

    }



}
